package otheri.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.zip.ZipFile;

import otheri.io.Input;
import otheri.io.Output;

public class Utils {

	public static void println(String line) {
		System.out.println("----------" + line);
	}

	public static String decodeDownloadContent(String content) {
		try {
			// Base64解码
			byte[] data = Base64.decode(content);

			// 还原byte数组
			byte[] newData = new byte[data.length];
			for (int i = 0; i < data.length; i++) {
				newData[i] = data[data.length - 1 - i];
			}

			// 读取两个字符串

			ByteArrayInputStream bais = new ByteArrayInputStream(newData);
			Input in = new Input(bais);
			String ret = in.readUTF8();
			in.close();
			bais.close();

			return ret;
		} catch (Exception e) {
			return null;
		}
	}

	public static String encodeDownloadContent(String url) {
		try {
			// 写入字符串
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			Output out = new Output(baos);
			out.writeUTF8(url);
			out.flush();
			baos.flush();
			byte[] data = baos.toByteArray();
			out.close();
			baos.close();

			// 进行转置，防止人家看到url
			byte[] newData = new byte[data.length];
			for (int i = 0; i < data.length; i++) {
				newData[i] = data[data.length - 1 - i];
			}

			// Base64编码
			return Base64.encodeBytes(newData);
		} catch (Exception e) {
			return null;
		}
	}


}
